using System.Diagnostics;

namespace SharpTalk.Client.WpfApplication.ViewModels
{
    using System.Collections.Generic;
    using System.Reflection;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;
    using Core;
    using Core.Enums;
    using Core.Services;
    using Views;

    public sealed class ChatRoomDetailsViewModel : ViewModelBase<IChatRoomView>
    {
        private bool _isNew = false;
        private byte[] _passwordHash;
        private bool _passwordDefined;

        private ChatRoomDetails _chatroomDetails;
        private readonly List<string> _chatRoomTypes = new List<string>()
                                                           {
                                                               Resources.Messages.chatroom_type_public,
                                                               Resources.Messages.chatroom_type_private,
                                                           };
        private readonly ICommand _createNewCommand = null;
        private readonly ICommand _definePasswordCommand = null;
        private readonly ICommand _clearPasswordCommand = null;

        public ChatRoomDetailsViewModel(IChatRoomView view)
            : base(view)
        {
            View.DataContext = this;
            View.SetIcon(Helpers.LoadImage("talk_16.png"));
        }
        
        public ICommand CreateNewCommand
        {
            get { return _createNewCommand ?? new BindableCommand<object>(OnCreateNew); }
        }
        public ICommand DefinePasswordCommand
        {
            get { return _definePasswordCommand ?? new BindableCommand<object>(OnDefinePassword); }
        }
        public ICommand ClearPasswordCommand
        {
            get { return _clearPasswordCommand ?? new BindableCommand<object>(OnClearPassword); }
        }
        public ChatRoomDetails ChatRoomDetails
        {
            get { return _chatroomDetails; }
            set
            {
                _chatroomDetails = value;
                NotifyPropertyChange(() => ChatRoomDetails, () => HasChatRoom);
                NotifyPropertyChange(() => IsProtected);
            }
        }

        public bool IsNew
        {
            get { return _isNew; }
            set { _isNew = value; NotifyPropertyChange(()=>IsNew); }
        }

        public List<string> ChatRoomTypes
        {
            get { return _chatRoomTypes; }
        }

        public bool HasChatRoom
        {
            get { return ChatRoomDetails != null; }
        }

        public bool HasPassword
        {
            get { return PasswordHash !=null; }
        }

        public byte[] PasswordHash
        {
            get { return _passwordHash ; }
        }

        public bool IsProtected
        {
            get { return HasChatRoom && ChatRoomDetails.Type.HasFlag(ChatRoomTypeEnum.Protected); }
        }

        public bool PasswordDefined
        {
            get { return _passwordDefined; }
        }

        public List<BitmapImage> Images
        {
            get{ return new List<BitmapImage>(); }
        }

        internal override void OnValidate(object value)
        {
            if (ShouldTrace(TraceEventType.Verbose))TraceMethodCall(MethodBase.GetCurrentMethod());
            base.OnValidate(value);
        }

        private void OnCreateNew(object value)
        {
            if (ShouldTrace(TraceEventType.Verbose))TraceMethodCall(MethodBase.GetCurrentMethod());

            _passwordHash = null;
            _passwordDefined = false;

            ChatRoomDetails = new ChatRoomDetails()
                                  {
                                      Name = Resources.Messages.label_new,
                                      Type = ChatRoomTypeEnum.Public
                                  };
            IsNew = true;
        }

        private void OnDefinePassword(object value)
        {
            if (ShouldTrace(TraceEventType.Verbose))TraceMethodCall(MethodBase.GetCurrentMethod());
            var vm = new PasswordViewModel(new PasswordView());
            var result = vm.View.ShowDialog((WindowBase) View);
            if (!result.HasValue || !result.Value) return;

            _passwordHash = vm.PasswordHash;
            if (!ChatRoomDetails.Type.HasFlag(ChatRoomTypeEnum.Protected))
                ChatRoomDetails.Type |= ChatRoomTypeEnum.Protected;

            _passwordDefined = true;
            NotifyPropertyChange(()=>IsProtected);
        }
       
        private void OnClearPassword(object value)
        {
            if (ShouldTrace(TraceEventType.Verbose)) TraceMethodCall(MethodBase.GetCurrentMethod());
            _passwordHash = null;
            _passwordDefined = true;

            if (ChatRoomDetails.Type.HasFlag(ChatRoomTypeEnum.Protected))
                ChatRoomDetails.Type ^= ChatRoomTypeEnum.Protected;

            NotifyPropertyChange(() => IsProtected);
        }
    }
}